package cn.wenhaha.app.data.service.imp;

import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.data.bean.DataPlugin;
import cn.wenhaha.app.data.bean.vo.DataPageURL;
import cn.wenhaha.app.data.service.DataPluginService;
import cn.wenhaha.app.data.service.DataUserService;
import cn.wenhaha.common.exception.ApiMSGException;
import cn.wenhaha.common.utils.PluginUtils;
import cn.wenhaha.datasource.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-07-14 22:18
 */
@Service
public class DataUserServiceImp implements DataUserService {


    @Resource
    private DataPluginService dataPluginService;




    @Override
    public List<DataPageURL> addPageList() {
        return dataPluginService.getDataPlugins().stream().map(m->{
            DataPageURL dataPageURL = new DataPageURL();
            dataPageURL.setCode(m.getCode());
            dataPageURL.setName(m.getName());
            dataPageURL.setIcon(m.getDataSource().getIcon());
            dataPageURL.setUrl("/plugins/"+m.getCode()+"/"+m.getAddPage());
            return  dataPageURL;
        }).collect(Collectors.toList());
    }



    @Override
    public List<DataUser> getUserList() {
        return dataPluginService.getDataPlugins().stream()
                .filter(m->   m.getDataSourcePlugin().getUserContext()!=null)
                .flatMap(m -> m.getDataSourcePlugin().getUserContext().list().stream())
                .collect(Collectors.toList());
    }


    @Override
    public String getUpdateUrl(String code, String id) {
        List<String> updateList = dataPluginService.getDataPlugins().stream().filter(d -> StrUtil.equals(d.getCode(), code))
                .map(DataPlugin::getUpdatePage).collect(Collectors.toList());
        if(updateList.isEmpty()){
            return "404";
        }
        return "/plugins/"+code+"/"+updateList.get(0)+"?id="+id;
    }

    @Override
    public  List<Obj> objectList(String code, String id) {
        DataPlugin dataPlugin = getDataSourcePlugin(code);
        IDataObject dataObject = dataPlugin.getDataSourcePlugin().getDataObject();
        return dataObject.list(id);
    }

    @Override
    public ObjInfo objectInfo(String code, String id, String apiName) {
        DataPlugin dataPlugin = getDataSourcePlugin(code);
        IDataObject dataObject = dataPlugin.getDataSourcePlugin().getDataObject();
        return dataObject.info(apiName,id);
    }

    @Override
    public Boolean remove(String code, String id) {
        DataPlugin dataPlugin =  getDataSourcePlugin(code);
        return dataPlugin.getDataSourcePlugin().getUserContext().removeUser(id);
    }


    private  DataPlugin getDataSourcePlugin(String code){
      return   dataPluginService.getDataPlugins().stream()
              .filter(m -> m.getDataSourcePlugin().getUserContext() != null)
              .filter(m -> StrUtil.equals(m.getCode(), code))
              .findFirst().<ApiMSGException>orElseThrow(() -> {
                  throw ApiMSGException.build(7009);
              });
    }
}
